<template>
  <a-modal
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :title="title"
    :visible="visible"
    :width="900"
    switchFullscreen
    okText="提交"
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <template slot="footer">
      <div>
        <a-button type="primary" @click="handleOk">新增</a-button>
        <a-button @click="close">关闭</a-button>
      </div>
    </template>

    <a-card :bordered="false">
      <a-form :form="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
          <!--          <a-col :sm="12" :xs="24">-->
          <a-form-item label="企业名称" prop="enterpriseName">
            <a-input
              v-model="model.enterpriseName"
              placeholder="请输入企业名称"
            />
          </a-form-item>
          <!--          </a-col>-->

          <a-form-item label="企业名缩写" prop="enterpriseShort">
            <a-input
              v-model="model.enterpriseShort"
              placeholder="请输入企业名缩写"
            />
          </a-form-item>

          <a-form-item label="统一社会信用代码" prop="enterpriseUscc">
            <a-input
              v-model="model.enterpriseUscc"
              placeholder="请输入统一社会信用代码"
            />
          </a-form-item>
          <a-form-item label="销售模式" prop="manageType">
            <a-radio-group name="radioGroup" v-model="model.manageType" :default-value="'0'" @change="radioChange">
              <a-radio :value="'0'">
                代理
              </a-radio>
              <a-radio :value="'1'">
                直销
              </a-radio>
            </a-radio-group>
            <!--              <a-input-->
            <!--                v-model="model.enterpriseUscc"-->
            <!--                placeholder="请输入销售模式"-->
            <!--              />-->
          </a-form-item>
          <a-form-item label="是否网签" prop="isWq">
            <a-radio-group name="radioGroup" v-model="model.isWq" :default-value="'1'">
              <a-radio :value="'1'">
                是
              </a-radio>
              <a-radio :value="'0'">
                否
              </a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="企业类型" prop="enterpriseType">
            <a-radio-group name="radioGroup" v-model="model.enterpriseType" :default-value="'1'">
              <a-radio :value="'1'">
                集团
              </a-radio>
              <a-radio :value="'2'">
                经销商
              </a-radio>
              <a-radio :value="'3'">
                厂商
              </a-radio>
            </a-radio-group>
          </a-form-item>
          <!--          <a-col :sm="12" :xs="24" v-show="model.enterpriseType == '2'">-->
          <a-form-item v-show="model.enterpriseType == '2'" label="选择集团" prop="usccPath">
            <!--              <a-select v-model="model.isSeparate">-->
            <!--                <a-select-option value="0">-->
            <!--                  已脱离集团-->
            <!--                </a-select-option >-->
            <!--                <a-select-option value="1">-->
            <!--                  未脱离集团-->
            <!--                </a-select-option >-->

            <!--              </a-select>-->
<!--            <a-input v-model="model.usccPath" style="width: 80%"/>-->
            <a-select
              v-model="model.defaultValue"
              placeholder="请选择集团"
              @change="changeINC"
            >
              <a-select-option
                v-for="item in enterpriseList"
                :key="item.id"
                :value="item.enterpriseUscc"
              >{{ item.enterpriseName }}
              </a-select-option>
            </a-select>
<!--            <a-button style="margin-left: 10px" @click="GetCompanymessage">获取集团信息</a-button>-->
          </a-form-item>
          <!--          </a-col>-->

          <a-form-item label="负责人姓名" prop="contactPerson">
            <a-input
              v-model="model.contactPerson"
              placeholder="请输入负责人姓名"
            />
          </a-form-item>

          <a-form-item label="负责人手机" prop="contactPhone">
            <a-input
              v-model="model.contactPhone"
              placeholder="请输入负责人手机"
            />
          </a-form-item>

          <a-form-item label="代理品牌" prop="brands">
            <a-input
              v-model="model.brands"
              placeholder="请输入代理品牌"
            />
          </a-form-item>

          <a-form-item label="所属区域" prop="registerDistinct">
            <a-input
              v-model="model.registerDistinct"
              placeholder="请输入所属区域"
            />
          </a-form-item>

          <a-form-item label="注册地址" prop="registerAddr">
            <a-input
              v-model="model.registerAddr"
              placeholder="请输入注册地址"
            />
          </a-form-item>

          <a-form-item label="经营地址" prop="operationAddr">
            <a-input
              v-model="model.operationAddr"
              placeholder="请输入经营地址"
            />
          </a-form-item>

          <a-form-item label="交付地点" prop="deliverAddress">
            <a-input
              v-model="model.deliverAddress"
              placeholder="请输入交付地点"
            />
          </a-form-item>

        </a-row>
      </a-form>
    </a-card>

  </a-modal>

</template>

<script>
import { httpAction, getAction } from '@api/manage'
import vue from 'vue'

export default {
  name: 'newEnterpriseMessage',
  data() {
    return {
      record: '',
      model: {
        manageType: '0',
        enterpriseType: '1',
        isWq: '1'
      },
      title: '新增信息',
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      wrapperCol: { span: 19 },
      labelCol: { span: 5 },
      enterpriseList: [],
      nowSelectedCode: '',
      nowSelectedPid: '',
      rules: [
        {
          enterpriseName: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
          enterpriseUscc: [{ required: true, message: '请输入统一社会信用代码', trigger: 'blur' }],
          enterpriseType: [{ required: true, message: '请选择企业类型', trigger: 'blur' }],
          usccPath: [{ required: false, message: '请选择集团', trigger: 'blur' }],
          contactPerson: [{ required: true, message: '请输入负责人姓名', trigger: 'blur' }],
          contactPhone: [{ required: true, message: '请输入负责人手机', trigger: 'blur' }],
          brands: [{ required: true, message: '请输入代理品牌', trigger: 'blur' }],
          isWq: [{ required: true, message: '请选择是否网签', trigger: 'blur' }],
          deliverAddress: [{ required: true, message: '请输入交付地址', trigger: 'blur' }],
          enterpriseShort: [{ required: true, message: '请输入企业名缩写', trigger: 'blur' }],
          registerDistinct: [{ required: true, message: '请输入所属区域', trigger: 'blur' }],
          registerAddr: [{ required: true, message: '请输入注册地址', trigger: 'blur' }],
          operationAddr: [{ required: true, message: '请输入经营地址', trigger: 'blur' }],
          manageType: [{ required: true, message: '请选择销售模式', trigger: 'blur' }]
        }
      ],
      url: {
        add: '/cxm/cxmEnterprise/add'
      }
    }

  },

  created() {
    this.GetCompanymessage()
  },

  methods: {
    changeINC(value, option) {
      console.log(value, option.key)
      this.nowSelectedCode = value
      // 选择的 统一信用代码
      this.nowSelectedPid = option.key
      // 选择的 id

    },
    open(record) {
      this.visible = true
      this.record = record

      // this.model = Object.assign({}, record)

    },

    GetCompanymessage() {
      // let httpurl = '/cxm/cxmEnterprise/queryEnterpriseInfo' + '?enterpriseUscc=' + code
      let httpurl = '/cxm/cxmEnterprise/getGroupEnterprise'
      getAction(httpurl, {})
        .then((res) => {
          console.log(res)
          if (res.success) {
            if (res.result !== null) {
              // this.$message.success('查询成功！')
              this.enterpriseList = res.result
              console.log(this.enterpriseList)

              //赋值
              // this.model.usccPath = res.result[0].enterpriseUscc
              //
              // this.model.enterpriseName = res.result.enterpriseName//企业名称
              // this.model.enterpriseType = res.result.enterpriseType//企业类型
              // this.model.isWq = res.result.isWq//是否网签
              // this.model.manageType = res.result.manageType//经营模式
              // this.model.contactPerson = res.result.contactPerson//负责人
              // this.model.contactPhone = res.result.contactPhone//负责人手机
              // this.model.brands = res.result.brands//代理品牌
              // this.model.deliverAddress = res.result.deliverAddress//交付地址
              // this.model.operationAddr = res.result.operationAddr//经营地址
              // this.model.registerAddr = res.result.registerAddr//注册地址
              // this.model.registerDistinct = res.result.registerDistinct//所属区域

            } else {
              this.$message.warn('集团信息查询为空，请核对统一社会信用代码！')
            }

          } else {
            this.$message.warn(res.result.message)
          }
        })
    },

    radioChange(e) {
      console.log('radio checked', e.target.value)
    },

    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.model = {
        manageType: '0',
        enterpriseType: '1',
        isWq: '1'
      }
      this.nowSelectedCode = ''
      this.nowSelectedPid = ''
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {

        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          httpurl += this.url.add
          method = 'post'
          // let formData = Object.assign(this.model, values)
          let cxmEnterprise = {
            enterpriseName: this.model.enterpriseName,//企业名称
            enterpriseUscc: this.model.enterpriseUscc,//统一信用代码
            manageType: this.model.manageType,//销售模式
            isWq: this.model.isWq,//是否网签
            enterpriseType: this.model.enterpriseType,//企业类型
            usccPath: this.nowSelectedCode,//集团-统一信用代码
            parentId: this.nowSelectedPid,//集团-统一信用代码
            contactPerson: this.model.contactPerson,//负责人姓名
            contactPhone: this.model.contactPhone,//负责人手机
            brands: this.model.brands,//代理品牌
            registerDistinct: this.model.registerDistinct,//所属区域
            registerAddr: this.model.registerAddr,//注册地址
            operationAddr: this.model.operationAddr,//经营地址
            deliverAddress: this.model.deliverAddress,//交付地点
            enterpriseShort: this.model.enterpriseShort//企业名缩写

          }
          // console.log(formData.manageType)

          // console.log(cxmEnterprise.usccPath)
          // console.log(cxmEnterprise.parentId)

          httpAction(httpurl, cxmEnterprise, method)
            .then((res) => {
              console.log(res)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
              that.$parent.$parent.loadData()
            })
        }
      })

    }

  }
}
</script>

<style scoped>

</style>